package com.lv.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Map;

/**
 * Created by lvjilei on 2018/4/4.
 */
public class HttpSendRequest {


    /**
     * 处理get请求
     * @param httpUrl 已拼接好参数 url?name1=value1&name2=value2
     * @return
     */
    public String sendGetReqeust(String httpUrl){
        HttpURLConnection connection = null;
        URL url = null;
        try {
            url = new URL(httpUrl);
            connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");
            connection.setRequestProperty("Accept","*/*");
            connection.setRequestProperty("Accept-Charset","UTF-8");
            connection.setRequestProperty("connection","Keep-Alive");
            connection.setRequestProperty("Content-Type","text/html;chartset=utf-8");

            connection.connect();
            BufferedReader br  = new BufferedReader(
                    new InputStreamReader(
                            connection.getInputStream(),"UTF-8"));
            StringBuffer res = new StringBuffer("");
            String line = "";
            while(null != (line = br.readLine())){
                res.append(line);
            }
            return res.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 处理post请求
     * @param httpUrl
     * @param params 参数形式 name1=value1&name2=value2
     * @return
     */
    public static String sendPost(String httpUrl ,String params){
        HttpURLConnection connection = null;
        URL url = null;
        OutputStreamWriter opsw = null;
        try {
            url = new URL(httpUrl);
            connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("POST");
            connection.setDoInput(true);
            connection.setDoOutput(true);
            connection.setUseCaches(false);
            connection.setRequestProperty("Accept","*/*");
            connection.setRequestProperty("Accept-Charset","UTF-8");
            connection.setRequestProperty("Content-Type","text/html;chartset=utf-8");

            opsw = new OutputStreamWriter(connection.getOutputStream(),"utf-8");
            opsw.write(params);
            opsw.flush();

            BufferedReader br  = new BufferedReader(
                    new InputStreamReader(
                            connection.getInputStream(),"UTF-8"));
            StringBuffer res = new StringBuffer("");
            String line = "";
            while(null != (line = br.readLine())){
                res.append(line);
            }
            return res.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    public static String sendRequestPost(String httUrl, Map<String,String> parmas){

        return null;


    }

}
